{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AF2算法\n",
    "\n",
    "- $N_{res} $:一级序列残基数量(在训练期间裁剪)\n",
    "- $N_{templ} $:模型中的模板数量\n",
    "- $N_{all\\_seq} $:所有可获得的MSA序列数量\n",
    "- $N_{clust} $:MSA聚类后的簇数量\n",
    "- $N_{seq} $:MSA堆叠过程中的序列数量,=$N_{clust}+N_{templ} $\n",
    "- $N_{extra\\_seq} $:子采样后未聚类的MSA序列数量\n",
    "- $N_{block} $:神经层堆叠数量\n",
    "- $N_{ensemble} $:组合的迭代数量\n",
    "- $N_{cycle} $:循环迭代次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections.abc import Sequence\n",
    "from random import randint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_res: int\n",
    "n_templ: int\n",
    "n_all_seq: int\n",
    "n_clust: int\n",
    "n_seq: int\n",
    "n_extra_seq: int\n",
    "n_block: int\n",
    "n_ensemble: int\n",
    "n_cycle:int"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MSA块删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def msa_block_deletion(msa:Sequence) -> Sequence:\n",
    "    '''对按顺序排列的MSA块进行随机连续块删除,提取多样性的子块'''\n",
    "    n_all_seq=len(msa)\n",
    "    block_size=int(0.3*n_all_seq)\n",
    "    to_delete =set()\n",
    "    for _ in range(5):\n",
    "        block_start=randint(1,n_all_seq)\n",
    "        to_delete.update(range(block_start,block_start+block_size)) # 不包括最后一个元素,总共添加block_size个元素\n",
    "    msa=[seq for idx,seq in enumerate(msa) if idx in to_delete]\n",
    "    return msa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 14, 28, 26, 31, 54, 50, 66, 55, 77, 102, 70, 123, 117]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[len(msa_block_deletion(list(range(_)))) for _ in range(10,150,10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n",
       " [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n",
       " [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[msa_block_deletion(list(range(20))) for _ in range(3)]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cctbx",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
